package edu.hawaii.halealohacli;

import java.util.List;
import java.util.Scanner;
import java.util.SortedMap;
import java.util.TreeMap;
import org.wattdepot.client.WattDepotClient;
import edu.hawaii.halealohacli.processor.CommandProcessor;

/**
 * Main class for the HaleAloha program.
 * 
 * @author Group BackR
 * 
 */
public class HaleAloha {

  /** The connected WattDepotClient. */
  public WattDepotClient client;
  
  private static SortedMap<String, List<Double>> baseLine = new TreeMap<String, List<Double>>();;


  /**
   * Gets keyboard input from user and splits the input by a space character.
   * 
   * @return The user input in a string array.
   */
  public String getInput() {
    // Display '>' for each input line.
    System.out.print("> ");
    Scanner scan = new Scanner(System.in);
    // Return user input.
    return scan.nextLine();
  }

  /**
   * Connects to a WattDepot server.
   * 
   * @param url URL to a running WattDepot server.
   * @return true Connected to the server. false Unable to connect to server.
   */
  public boolean connect(String url) {
    client = new WattDepotClient(url);
    // Check to make sure a connection can be made.
    // If no connection, then exit right now.
    if (client.isHealthy()) {
      System.out.println("Connected successfully to the Hale Aloha WattDepot server.");
      return true;
    }
    else {
      System.out.format("Could not connect to: %s%n", url);
      return false;
    }
  }

  /**
   * Main program loop.
   * @param connect If the program successfully connected to the client.
   * @param input The user input.
   * @return False if user exits the program.
   */
  public boolean program(boolean connect, String input) {
    if (connect) {
      // Create new instance of the CommandProcessor.
      CommandProcessor processor = new CommandProcessor();
        // Send input to processor to process and get the output.
        String output = processor.process(input, client);
        // Break loop if a null output is returned.
        if (output == null) {
          return false;
        }
        // Print out the output and continue the loop.
        else {
          System.out.println(output);
          return true;
        }
    }
    return false;
  }
  
  /**
   * get the baseline.
   * @return the baseline.
   */
  public static SortedMap<String, List<Double>> getBaseLine () {
    return HaleAloha.baseLine;
  }
  /**
   * The main method of the program. Creates a new HaleAloha instance.
   * 
   * @param args Override default WattDepot server URL.
   */
  public static void main(String[] args) {
    HaleAloha haleAloha = new HaleAloha();
    
    String url = "http://server.wattdepot.org:8190/wattdepot/";
    if (args.length == 1) {
      url = args[0];
    }
    boolean connect = haleAloha.connect(url);
    while (connect) {
      connect = haleAloha.program(connect, haleAloha.getInput());
    }
    // Provide message that program has terminated.
    System.out.println("Program sucessfully terminated.");
  }
}
